<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/9 0009
 * Time: 下午 5:12
 */
namespace app\common;

class BaseHelper{
    /**
     * curl POST方式实现
     * @param $url
     * @param $postData
     * @return mixed
     * @author fei <xliang.fei@gmail.com>
     */
    public static function curlPost($url, $postData)
    {
        //初始化
        $curl = curl_init(); //用curl发送数据给api
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        $response = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $response;
    }

    /**
     * curl GET方式实现
     * @param $url
     * @return mixed
     * @author fei <xliang.fei@gmail.com>
     */
    public static function curlGet($url)
    {
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $data;
    }

    //生成唯一ID
    public static function getUUID($prefix=''){
        $uuid = md5(uniqid(mt_rand(), true));
        return $prefix . $uuid;
    }

    public static function getUUID22(){
        $uuid = md5(uniqid(mt_rand(), true));
        return substr($uuid,mt_rand(0,10),22);
    }

    //把其它地图的坐标转换成腾讯地图的坐标
    public static function CoordinateConvert($latlng){
        $url = sprintf(config('map.TencentMapUrl'),$latlng,config('map.type'),config('map.key'));
        $res = json_decode(BaseHelper::curlGet($url),true);
        return $res['locations'][0]['lat'].','.$res['locations'][0]['lng'];
    }

    public static function GeocoderAddress($address){
        $url = 'http://apis.map.qq.com/ws/geocoder/v1/?address=' . $address . '&key=' . config('map.key');
        $res = json_decode(BaseHelper::curlGet($url),true);
        return $res['result'];
    }




    //某个值是否存在多维数组中
    /**
     * @param $value
     * @param $array
     * @return bool
     */
    public static function IsGetArray($value, $array){
        foreach($array as $item) {
            if(!is_array($item)) {
                if ($item == $value) {
                    return true;
                } else {
                    continue;
                }
            }

            if(in_array($value, $item)) {
                return true;
            } else if(self::IsGetArray($value, $item)) {
                return true;
            }
        }
        return false;
    }

    //打印机商品名格式函数

    /**
     * @param $name 商品名
     * @param $type 打印机尺寸 1表示58mm打印机  2表示80mm打印机
     * @param string $kw3
     * @return string
     */
    public static function printCaiName($name,$type,$kw3=''){
        $name = iconv("UTF-8", "GBK//IGNORE", $name);
        if($type==1){
            if(strlen($name)>10){
                $k = 12 - strlen(substr($name,10,10));
                if($k > 2){
                    $k = $k+3;
                    if($k > 7){
                        $k += $k - 7;
                    }
                }
                for($q=0;$q<$k;$q++){
                    $kw3 .= ' ';
                }
                if($k=='2'){
                    $kw3 .= ' ';
                }
                $name = '<B>'.substr($name,0,10).'</B><BR><B>'.substr($name,10,10).'</B>'.$kw3;
            }else{
                $k = 12 - strlen($name);
                if($k > 2){
                    $k = $k+3;
                    if($k > 7){
                        $k += $k - 7;
                    }
                }
                for($q=0;$q<$k;$q++){
                    $kw3 .= ' ';
                }
                if($k=='2'){
                    $kw3 .= ' ';
                }
                $name = '<B>'.$name.'</B>'.$kw3;
            }
            $name = iconv("GBK", "UTF-8//IGNORE", $name);
        }else{
            if(strlen($name)>16){
                $k = 18 - strlen(substr($name,16,16));
                if($k > 2){
                    $k = $k+3;
                    if($k > 7){
                        $k += $k - 7;
                    }
                }
                for($q=0;$q<$k;$q++){
                    $kw3 .= ' ';
                }
                if($k=='2'){
                    $kw3 .= ' ';
                }
                $name = '<B>'.substr($name,0,16).'</B><BR><B>'.substr($name,16,16).'</B>'.$kw3;
            }else{
                $k = 18 - strlen($name);
                if($k > 2){
                    $k = $k+3;
                    if($k > 7){
                        $k += $k - 7;
                    }
                }
                for($q=0;$q<$k;$q++){
                    $kw3 .= ' ';
                }
                if($k=='2'){
                    $kw3 .= ' ';
                }
                $name = '<B>'.$name.'</B>'.$kw3;
            }
            $name = iconv("GBK", "UTF-8//IGNORE", $name);
        }

        return $name;
    }

    /**
     * 后台登录密码加密
     *
     * @param $password 要加密的密码
     * @param string $algo 要使用的哈希算法，例如："md5"，"sha256"，"haval160,4" 等
     * @param string $key 加密秘钥
     * @return string
     */
    public static function passwordEncrypt($password, $algo = 'ripemd256', $key = 'mxkj@2018'){
        $password = hash($algo, $key . $password);

        return $password;
    }

}